package cs422.group1.projectsquirrel;

import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ExtraSurvey extends Activity {

	
	//private TextView _test;
	//Create references to views on current screen.
	private Spinner observationSpinner;
	private Spinner nutSpinner;
	private Spinner seedSpinner;
	private Spinner fruitSpinner; 
	private Spinner tinySpinner;
	private Spinner coniferousSpinner;
	private Button nextButton;

	
	
	//Information gathered from Extended Survey Page 1
	private String valueOfSpinner;
	
	private JavaSucks observationValue, nutValue, seedValue, fruitValue, tinyValue, coniferousValue;


	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.extrasurveypage1);
        

        //Setting up the references to all of the elements on the current screen.
        nextButton = (Button)findViewById(R.id.nextButton1);
        observationSpinner = (Spinner)findViewById(R.id.observationSpinner);
        nutSpinner = (Spinner)findViewById(R.id.nutSpinner);
        seedSpinner = (Spinner)findViewById(R.id.seedSpinner);
        fruitSpinner = (Spinner)findViewById(R.id.fruitSpinner);
        tinySpinner = (Spinner)findViewById(R.id.spinner1);
        coniferousSpinner = (Spinner)findViewById(R.id.spinner2);
        
        observationValue = new JavaSucks("");
        nutValue = new JavaSucks("");
        seedValue = new JavaSucks("");
        fruitValue = new JavaSucks("");
        tinyValue = new JavaSucks("");
        coniferousValue = new JavaSucks("");
        
        
        //Setting up the drop down menu and attaching a listner.
        //Observation Setting Spinner 
        ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(
        		this, R.array.settingsArray, R.layout.spinner_style);
        spinnerAdapter.setDropDownViewResource(R.layout.combo_style);
        observationSpinner.setAdapter(spinnerAdapter);
        observationSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(observationValue));
        
        //Nut Spinner 
        ArrayAdapter<CharSequence> nutAdapter = ArrayAdapter.createFromResource(
        		this, R.array.YesNoNA, R.layout.spinner_style);
        nutAdapter.setDropDownViewResource(R.layout.combo_style);
        nutSpinner.setAdapter(nutAdapter);
        nutSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(nutValue));
        
        
        //Seed Spinner
        ArrayAdapter<CharSequence> seedAdapter = ArrayAdapter.createFromResource(
        		this, R.array.YesNoNA, R.layout.spinner_style);
        seedAdapter.setDropDownViewResource(R.layout.combo_style);
        seedSpinner.setAdapter(seedAdapter);
        seedSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(seedValue));        

        //Fruit Spinner
        ArrayAdapter<CharSequence> fruitAdapter = ArrayAdapter.createFromResource(
        		this, R.array.YesNoNA, R.layout.spinner_style);
        fruitAdapter.setDropDownViewResource(R.layout.combo_style);
        fruitSpinner.setAdapter(fruitAdapter);
        fruitSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(fruitValue));
        
        //Tiny Spinner
        ArrayAdapter<CharSequence> tinyAdapter = ArrayAdapter.createFromResource(
        		this, R.array.YesNoNA, R.layout.spinner_style);
        tinyAdapter.setDropDownViewResource(R.layout.combo_style);
        tinySpinner.setAdapter(tinyAdapter);
        tinySpinner.setOnItemSelectedListener(new myOnItemSelectedListener(tinyValue));
        
        //Coniferous Spinner
        ArrayAdapter<CharSequence> coniferousAdapter = ArrayAdapter.createFromResource(
        		this, R.array.YesNoNA, R.layout.spinner_style);
        coniferousAdapter.setDropDownViewResource(R.layout.combo_style);
        coniferousSpinner.setAdapter(coniferousAdapter);
        coniferousSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(coniferousValue)); 
        
        if (ObservationDataManager.isEdit())
        {
        	observationSpinner.setSelection(spinnerAdapter.getPosition(ObservationDataManager.getData().setting));
        	nutSpinner.setSelection(nutAdapter.getPosition(ObservationDataManager.getData().nut_trees));
        	seedSpinner.setSelection(seedAdapter.getPosition(ObservationDataManager.getData().seed_trees));
        	fruitSpinner.setSelection(fruitAdapter.getPosition(ObservationDataManager.getData().fruit_trees));
        	tinySpinner.setSelection(tinyAdapter.getPosition(ObservationDataManager.getData().tiny_trees));
        	coniferousSpinner.setSelection(coniferousAdapter.getPosition(ObservationDataManager.getData().con_trees));
        	
        }

        
        //Attaching a listener for callback on the next button.
        nextButton.setOnClickListener(new nextQuestionListener());      
  
	}
	

	//Done with this activity, and moving on to the next one.
	private void nextPage()
	{
		Intent intent = new Intent(this.getBaseContext(),ExtraSurvey2.class);
		startActivity(intent);
	}
	
	//Gather data on current page and store it for the survey.
	private void gatherData()
	{
		ObservationData od = ObservationDataManager.getData();
		
		od.setting = observationValue.getValue();
		od.nut_trees = nutValue.getValue();
		od.seed_trees = seedValue.getValue();
		od.fruit_trees = fruitValue.getValue();
		od.tiny_trees = tinyValue.getValue();
		od.con_trees = coniferousValue.getValue();;
	}
	
	//Callback function for the drop down box on the current page.
	public class myOnItemSelectedListener implements OnItemSelectedListener
	{
		private JavaSucks val;
		public myOnItemSelectedListener(JavaSucks _val)
		{
			val = _val;
		}
		
		public void onItemSelected(AdapterView<?> parent,View view,int pos,long id)
		{
			  val.setValue(parent.getItemAtPosition(pos).toString());
		}
		public void onNothingSelected(AdapterView<?> parent)
		{
			//Do Nothing!
		}
	}
		
	//Callback function for the NEXT button.
	public class nextQuestionListener implements OnClickListener
	{
		public void onClick(View view)
		{
			gatherData();
			nextPage();
		}
	}
	
	//for nut trees ?
    public void getNut(View v)
    {
    	Intent intent = new Intent(this.getBaseContext(), NutTrees.class);
    	startActivity(intent);
    }
	
    //for seed trees ?
    public void getSeed(View v)
    {
    	Intent intent = new Intent(this.getBaseContext(), SeedTrees.class);
    	startActivity(intent);
    }
	
    //for fruit trees ?
    public void getFruit(View v)
    {
    	Intent intent = new Intent(this.getBaseContext(), FruitTrees.class);
    	startActivity(intent);
    }
    
    //for tiny seed trees ?
    public void getTiny(View v)
    {
    	Intent intent = new Intent(this.getBaseContext(), TinyTrees.class);
    	startActivity(intent);
    }
	
    //for coniferous trees ?
    public void getCon(View v)
    {
    	Intent intent = new Intent(this.getBaseContext(), ConTrees.class);
    	startActivity(intent);
    }
	
}
